{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check If a Word Occurs As a Prefix of Any Word in a Sentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #string-matching"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #字符串匹配"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isPrefixOfWord"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查单词是否为句中其他单词的前缀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>sentence</code> 作为句子并指定检索词为 <code>searchWord</code> ，其中句子由若干用 <strong>单个空格</strong> 分隔的单词组成。请你检查检索词 <code>searchWord</code> 是否为句子 <code>sentence</code> 中任意单词的前缀。</p>\n",
    "\n",
    "<p>如果&nbsp;<code>searchWord</code> 是某一个单词的前缀，则返回句子&nbsp;<code>sentence</code> 中该单词所对应的下标（<strong>下标从 1 开始</strong>）。如果 <code>searchWord</code> 是多个单词的前缀，则返回匹配的第一个单词的下标（<strong>最小下标</strong>）。如果 <code>searchWord</code> 不是任何单词的前缀，则返回 <code>-1</code><strong> </strong>。</p>\n",
    "\n",
    "<p>字符串 <code>s</code> 的 <strong>前缀</strong> 是 <code>s</code> 的任何前导连续子字符串。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>sentence = \"i love eating burger\", searchWord = \"burg\"\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>\"burg\" 是 \"burger\" 的前缀，而 \"burger\" 是句子中第 4 个单词。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>sentence = \"this problem is an easy problem\", searchWord = \"pro\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\"pro\" 是 \"problem\" 的前缀，而 \"problem\" 是句子中第 2 个也是第 6 个单词，但是应该返回最小下标 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>sentence = \"i am tired\", searchWord = \"you\"\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>\"you\" 不是句子中任何单词的前缀。\n",
    "\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= sentence.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= searchWord.length &lt;= 10</code></li>\n",
    "\t<li><code>sentence</code> 由小写英文字母和空格组成。</li>\n",
    "\t<li><code>searchWord</code> 由小写英文字母组成。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"i love eating burger\"\\n\"burg\"', '\"this problem is an easy problem\"\\n\"pro\"', '\"i am tired\"\\n\"you\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        targ=sentence.split(' ')\n",
    "        inde=-1\n",
    "        for i in targ:\n",
    "            if len(i)<len(searchWord):\n",
    "                pass\n",
    "            elif i[:len(searchWord)]==searchWord:\n",
    "                inde=targ.index(i)+1\n",
    "                break\n",
    "        return inde"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l = sentence.split()\n",
    "        for i in range(len(l)):\n",
    "            if l[i][:len(searchWord)] == searchWord:\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        length_target = len(searchWord)\n",
    "        for idx, word in enumerate(sentence.split()):\n",
    "            if len(word) < length_target:\n",
    "                continue\n",
    "            if word[:length_target] == searchWord:\n",
    "                return idx+1\n",
    "        return -1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for i,word in enumerate(sentence.split(' ')):\n",
    "            if word.startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        for i, word in enumerate(words, 1):\n",
    "            if word.startswith(searchWord):\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.split(' ')\n",
    "        for x in range(len(sentence)):\n",
    "            try:\n",
    "                if sentence[x].index(searchWord) == 0:\n",
    "                    return x + 1\n",
    "            except:\n",
    "                pass\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l1 = list(sentence.split(\" \"))\n",
    "        for i in range (len(l1)):\n",
    "            if len(l1[i]) >= len(searchWord) and l1[i][:len(searchWord)] == searchWord:\n",
    "                return i + 1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.split(' ')\n",
    "        for i in range(len(sentence)):\n",
    "            if sentence[i].startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l = sentence.split()\n",
    "        for i in range(len(l)):\n",
    "            if l[i][:len(searchWord)] == searchWord:\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.split()\n",
    "        for i in range(len(sentence)):\n",
    "            if searchWord in sentence[i][:len(searchWord)]:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        i,index,n=0,1,len(sentence)\n",
    "        while i<n:\n",
    "            start =i\n",
    "            while i<n and sentence[i]!=' ':\n",
    "                i+=1\n",
    "            end =i\n",
    "            if sentence[start:end].startswith(searchWord):\n",
    "                return index\n",
    "            index+=1\n",
    "            i+=1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence=sentence.split()\n",
    "        sentence_len=len(sentence)\n",
    "      \n",
    "        searchWord_len=len(searchWord)\n",
    "        ans=-1\n",
    "        for i in range(sentence_len):\n",
    "            t_len=len(sentence[i])\n",
    "            if t_len>=searchWord_len and str(searchWord)==str(sentence[i][0:searchWord_len]):                \n",
    "                    return i+1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "\n",
    "        def f(a,b):\n",
    "            if len(b)<len(a):\n",
    "                return False\n",
    "            for i in range(len(a)):\n",
    "                if a[i] != b[i]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "\n",
    "        l = sentence.split(\" \")\n",
    "        for i in range(len(l)):\n",
    "            if f(searchWord,l[i]):\n",
    "                return i+1\n",
    "        return -1\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        for i, word in enumerate(words):\n",
    "            if word[:len(searchWord)]==searchWord:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        ls = sentence.split(' ')\n",
    "        n = len(searchWord)\n",
    "        for i,v in enumerate(ls):\n",
    "            if len(v) < n:\n",
    "                continue\n",
    "            if v[:n] == searchWord:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l,idx,lenth = 0,0,len(searchWord)\n",
    "        for i in range(len(sentence)+1):\n",
    "            if i == len(sentence) or sentence[i] == ' ':\n",
    "                idx += 1\n",
    "                if sentence[l:l+lenth] == searchWord:\n",
    "                    return idx\n",
    "                l = i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.strip().split()\n",
    "        n = len(searchWord)\n",
    "        for i, v in enumerate(sentence):\n",
    "            if v[:n] == searchWord:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        t = sentence.split()\n",
    "        n = len(searchWord)\n",
    "        for i in range(len(t)):\n",
    "            if t[i][:n] == searchWord:\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(searchWord)):\n",
    "                if j >= len(words[i]) or words[i][j] != searchWord[j]:\n",
    "                    a = 1\n",
    "                    break\n",
    "                else:\n",
    "                    a = 0\n",
    "            if a == 0:\n",
    "                return i+1\n",
    "        return -1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        s=sentence.split(' ')\n",
    "        for i in s:\n",
    "            if searchWord in i and i[:len(searchWord)]==searchWord:\n",
    "                return s.index(i)+1\n",
    "                break\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        p = re.compile('^' + searchWord + '.*')\n",
    "        for i, v in enumerate(sentence.split()):\n",
    "            if p.match(v):\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        lst = sentence.split(\" \")\n",
    "        ln = len(searchWord)\n",
    "        ans = -1\n",
    "        for i in range(len(lst)):\n",
    "            if searchWord == lst[i][:ln]:\n",
    "                ans = i + 1\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        lenth=len(searchWord)\n",
    "        sentence=sentence.split()\n",
    "        list1=[]\n",
    "        for i in range(len(sentence)):\n",
    "            if len(sentence[i])>=lenth and sentence[i][:lenth]==searchWord:\n",
    "                list1.append(i+1)\n",
    "        if list1==[]:\n",
    "            return -1\n",
    "        else:\n",
    "            return min(list1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for i,word in enumerate(sentence.split(' ')):\n",
    "            if word.startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l = sentence.split()\n",
    "        n = len(searchWord) \n",
    "        for i in l :\n",
    "            if len(i) < n:\n",
    "                continue\n",
    "            if i[:n] == searchWord:\n",
    "                return l.index(i) + 1\n",
    "        return -1 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l=sentence.split()\n",
    "        n=len(searchWord)\n",
    "        for i,j in enumerate(l,1):\n",
    "            if n<=len(j) and searchWord==j[:n]:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split(' ')\n",
    "\n",
    "        length = len(searchWord)\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len(words)):\n",
    "            if words[i][:length] == searchWord:\n",
    "                return i + 1\n",
    "            \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        list1 = sentence.split(' ')\n",
    "        for i, sen in enumerate(list1):\n",
    "            if sen.startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        i, index, n = 0, 1, len(sentence)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n and sentence[i] != \" \":\n",
    "                i += 1\n",
    "            end = i\n",
    "            if sentence[start: end].startswith(searchWord):\n",
    "                return index\n",
    "            index += 1\n",
    "            i += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        i, index, n = 0, 1, len(sentence)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n and sentence[i] != ' ':\n",
    "                i += 1\n",
    "            end = i\n",
    "            if sentence[start:end].startswith(searchWord):\n",
    "                return index\n",
    "            index += 1\n",
    "            i += 1\n",
    "        return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        for i in range(len(words)):\n",
    "            if searchWord in words[i] and searchWord[:] == words[i][:len(searchWord)]:\n",
    "                return i + 1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sens = sentence.split()\n",
    "        n = len(sens)\n",
    "        for i in range(n):\n",
    "            if sens[i].startswith(searchWord):\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        a = sentence.split(' ')\n",
    "        n = len(searchWord)\n",
    "        for i, j in enumerate(a):\n",
    "            if len(j) < n:\n",
    "                continue\n",
    "            else:\n",
    "                if j[:n] == searchWord:\n",
    "                    return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.split()\n",
    "        for i in range(len(sentence)):\n",
    "            if sentence[i].find(searchWord)==0:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        n = len(sentence)\n",
    "\n",
    "        i = 0\n",
    "        index = 1\n",
    "        while i<n:\n",
    "            start = i\n",
    "            while i<n and sentence[i] != ' ':\n",
    "                i+=1\n",
    "            end = i\n",
    "            if sentence[start:end].startswith(searchWord):\n",
    "                return  index\n",
    "            index += 1\n",
    "            i += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "    \n",
    "        for i, word in enumerate(words):\n",
    "            if word.startswith(searchWord):\n",
    "                return i + 1\n",
    "    \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(searchWord)):\n",
    "                if j >= len(words[i]) or words[i][j] != searchWord[j]:\n",
    "                    a = 1\n",
    "                    break\n",
    "                else:\n",
    "                    a = 0\n",
    "            if a == 0:\n",
    "                return i+1\n",
    "        return -1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        s = list(sentence.split())\n",
    "        for i in range(len(s)):\n",
    "            if s[i].startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence1=sentence.split(\" \")\n",
    "        for i,j in enumerate(sentence1,1):\n",
    "            if j[:len(searchWord)]== searchWord:\n",
    "                return i\n",
    "        return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        ans = -1;\n",
    "        danciList = sentence.split(\" \")\n",
    "        for i in range(len(danciList)):\n",
    "            if searchWord in danciList[i]:\n",
    "                if danciList[i].find(searchWord) == 0:\n",
    "                    ans = i+1\n",
    "                    break\n",
    "        if ans == -1:\n",
    "            return -1\n",
    "        else:\n",
    "            return ans;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for i, word in enumerate(sentence.split(\" \"), 1):\n",
    "            if word[:len(searchWord)] == searchWord:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        tem = sentence.split(' ')\n",
    "        cnt = 1\n",
    "        for i in tem:\n",
    "            if searchWord in i and i.startswith(searchWord) is True:\n",
    "                return cnt\n",
    "            else:\n",
    "                cnt += 1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split(' ')\n",
    "        for i in range(len(words)):\n",
    "            if words[i][:len(searchWord)]==searchWord:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        ls = sentence.split(' ')\n",
    "        n = len(searchWord)\n",
    "        for i,v in enumerate(ls):\n",
    "            if len(v) < n:\n",
    "                continue\n",
    "            if v[:n] == searchWord:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        temp = sentence.split(' ')\n",
    "        m = len(temp)\n",
    "        n = len(searchWord)\n",
    "        for i in range(m):\n",
    "            if len(temp[i]) < n:\n",
    "                continue\n",
    "            judge = True\n",
    "            for j in range(n):\n",
    "                if searchWord[j] != temp[i][j]:\n",
    "                    judge = False\n",
    "            if judge == True:\n",
    "                return i + 1\n",
    "        return - 1\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sl=sentence.split()\n",
    "        for i in range(len(sl)):\n",
    "            if sl[i].find(searchWord)==0:\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        lst = list(sentence.split())\n",
    "        l = len(searchWord)\n",
    "        for i in lst:\n",
    "            if searchWord == i[:l]:\n",
    "                return lst.index(i) + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        res = -1\n",
    "        words = sentence.split()\n",
    "        for i,w in enumerate(words):\n",
    "            if w[:len(searchWord)] == searchWord:\n",
    "                return i+1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for num, word in enumerate(sentence.split(' '), 1):\n",
    "            if word[:len(searchWord)] == searchWord:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for i, word in enumerate(sentence.split()):\n",
    "            if word.startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        length_target = len(searchWord)\n",
    "        for idx, word in enumerate(sentence.split()):\n",
    "            if len(word) < length_target:\n",
    "                continue\n",
    "            if word[:length_target] == searchWord:\n",
    "                return idx+1\n",
    "        return -1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        for index, word in enumerate(sentence.split()):\n",
    "            if word.startswith(searchWord):\n",
    "                return index + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = sentence.split()\n",
    "        ans = -1\n",
    "        i = 1\n",
    "        for word in words:\n",
    "            if len(word) < len(searchWord):\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                if searchWord == word[:len(searchWord)]:\n",
    "                    ans = i\n",
    "                    break\n",
    "                else:\n",
    "                    i += 1\n",
    "                    continue\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence = sentence.split(' ')\n",
    "        for index,value in enumerate(sentence):\n",
    "            if searchWord == value[:len(searchWord)]:\n",
    "                return index + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        words = ['']+list(sentence.split()) # 用split以空格分割sentence\n",
    "                                            # 再用list转换为列表\n",
    "                                            # 由于题目下标从1开始，在前面随便加1个元素\n",
    "        p = len(searchWord) # 获取切片位置\n",
    "        for w in range(1,len(words)):\n",
    "            if words[w][:p]==searchWord:return w # 对每个元素进行切片，与searchword比较\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        order = 1\n",
    "        prev = ' '\n",
    "        for i in range(len(sentence)):\n",
    "            if sentence[i] == ' ':\n",
    "                order += 1\n",
    "            elif prev == ' ' and searchWord == sentence[i:i + len(searchWord)]:\n",
    "                return order\n",
    "            else:\n",
    "                pass\n",
    "            \n",
    "            prev = sentence[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        n = len(searchWord)\n",
    "        s = sentence.split()\n",
    "        for i in range(len(s)):\n",
    "            if s[i][: n] == searchWord:\n",
    "                return i + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        s=sentence.split(' ')\n",
    "        for i,x in enumerate(s):\n",
    "            if x.startswith(searchWord):\n",
    "                return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "       \n",
    "\n",
    "        sentence = sentence.split(' ')\n",
    "\n",
    "        \n",
    "        for i,sen in enumerate(sentence):\n",
    "            if len(sen) < len(searchWord):\n",
    "                continue\n",
    "            \n",
    "            if searchWord == sen[:len(searchWord)]:\n",
    "                return i + 1\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sentence_list = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(sentence_list):\n",
    "            if len(searchWord) > len(word):\n",
    "                continue\n",
    "            match = True\n",
    "            for j in range(len(searchWord)):\n",
    "                \n",
    "                if searchWord[j] != word[j]:\n",
    "                    match = False\n",
    "                    break\n",
    "            if match:\n",
    "                return i + 1\n",
    "            \n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        i, index, n = 0, 1, len(sentence)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n and sentence[i] != ' ':\n",
    "                i += 1\n",
    "            end = i\n",
    "            if sentence[start: end].startswith(searchWord):\n",
    "                return index\n",
    "            index += 1\n",
    "            i += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        i, index, n = 0, 1, len(sentence)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n and sentence[i] != ' ':\n",
    "                i += 1\n",
    "            end = i\n",
    "            if sentence[start:end].startswith(searchWord):\n",
    "                return index\n",
    "            index += 1\n",
    "            i += 1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        n = len(searchWord)\n",
    "        strs = sentence.split(\" \")\n",
    "        for i, s in enumerate(strs):\n",
    "            if len(s) < n:\n",
    "                continue\n",
    "            if s[:n] == searchWord:\n",
    "                return i+1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        l=len(searchWord)\n",
    "        words=sentence.split()\n",
    "        for i,x in enumerate(words):\n",
    "            if len(x)>=l:\n",
    "                if x[0:l]==searchWord:\n",
    "                    return i+1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        sen_list = sentence.split(' ')\n",
    "        for i in range(len(sen_list)):\n",
    "            if sen_list[i].startswith(searchWord):\n",
    "                return i + 1\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:\n",
    "        space = 0\n",
    "        for i in range(len(sentence)):\n",
    "            if sentence[i] == ' ':\n",
    "                space += 1\n",
    "                if sentence[i+1] == searchWord[0]:\n",
    "                    j = 0\n",
    "                    while sentence[i+1] == searchWord[j]:\n",
    "                        if j == len(searchWord) - 1:\n",
    "                            return space + 1\n",
    "                        i += 1\n",
    "                        j += 1\n",
    "            if i == 0:\n",
    "                if sentence[i] == searchWord[0]:\n",
    "                    j = 0\n",
    "                    while sentence[i] == searchWord[j]:\n",
    "                        if j == len(searchWord) - 1:\n",
    "                            return space + 1\n",
    "                        i += 1\n",
    "                        j += 1\n",
    "        return -1\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
